﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class PerCustmPriceChartController : BaseController<usersorder>
    {
        // 客单价  客单量走势图  
        // GET: /PerCustmPriceChart/ shopName PerPrice

        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }
        public class PerPriceModel
        {
            public string shopName { get; set; }
            public string PerPrice { get; set; }//客单价
            public int CustomerSum { get; set; }  //客单量
            public string Date { get; set; }
            public string orderSumprice { get; set; }


        }
        public override BLL.BaseBll<usersorder> bll
        {
            get
            {
                return new UsersOrderBll();
            }

        }

        StoreBll storeBll = new StoreBll();

        public decimal Actualmoney;//实际收入金额

        public int OrderSum;  //客单量  PerPrice OrderSum

        /// <summary>
        /// 客单价 统计图   客单量
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>

        public ActionResult PerCustomerPrice(string date1, string date2, string storeid)
        {
            var dataList = new List<PerPriceModel>();

            //   默认查询本天 之前的 一周的记录  总的店铺的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(storeid))
            {
                //先算6 天的   
                for (var i = 0; i <= 6; i++)
                {
                    var countData = i;
                    var query = bll.Search().ToList();
                    var listOrder = query.Where(x => x.orderTime.Date == DateTime.Now.Date.AddDays(-countData) && x.orderStatus == 1).GroupBy(x => x.orderStatus).Select(x => new { orderSumprice = x.Sum(y => y.orderSumprice), orderDis = x.Sum(y => y.discountMoney), Count = x.Count(), });


                    foreach (var oneShop in listOrder)
                    {
                        int a = oneShop.Count;
                        PerPriceModel orderModel = new PerPriceModel();
                        orderModel.Date = DateTime.Now.Date.AddDays(-countData).ToString("yy/MM/dd");
                        orderModel.CustomerSum = oneShop.Count; //单量
                        decimal d = Convert.ToDecimal(oneShop.orderDis);
                        orderModel.PerPrice = ((oneShop.orderSumprice + d) / a).ToString("#.##");
                        dataList.Add(orderModel);
                    }
                }
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);
            }
            //按店铺查询 
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != "" && storeid != null)
            {
                int storeID = Convert.ToInt32(storeid);
                //先算6 天的   
                for (var i = 0; i <= 6; i++)
                {
                    var countData = i;
                    var query = bll.Search().ToList();
                    var listOrder = query.Where(x => x.orderTime.Date == DateTime.Now.Date.AddDays(-countData) && x.orderStatus == 1 && x.storeid == storeID).GroupBy(x => x.orderStatus).Select(x => new { orderSumprice = x.Sum(y => y.orderSumprice), orderDis = x.Sum(y => y.discountMoney), Count = x.Count(), });
                    foreach (var oneShop in listOrder)
                    {
                        int a = oneShop.Count;
                        PerPriceModel orderModel = new PerPriceModel();
                        orderModel.Date = DateTime.Now.Date.AddDays(-countData).ToString("yy/MM/dd");
                        orderModel.CustomerSum = oneShop.Count; //单量
                        decimal d = Convert.ToDecimal(oneShop.orderDis);
                        orderModel.PerPrice = ((oneShop.orderSumprice + d) / a).ToString("#.##");
                        dataList.Add(orderModel);
                    }
                }
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);
            }
            //按日期查询 总的
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && string.IsNullOrEmpty(storeid))
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                TimeSpan sp = datetime2.Subtract(datetime1);  //这里验证通过  不在一个月的日期也可以  

                //相差天数
                int dayMin = sp.Days;
                for (var i = 0; i < dayMin; i++)
                {
                    var countData = i;

                    DateTime date11 = datetime1.Date.AddDays(+countData);

                    var query = bll.Search().ToList();

                    var listOrder = query.Where(x => x.orderTime.Date == date11.Date && x.orderStatus == 1).GroupBy(x => x.orderStatus).Select(x => new { x.FirstOrDefault().orderCode, orderSumprice = x.Sum(y => y.orderSumprice), orderDis = x.Sum(y => y.discountMoney), x.FirstOrDefault().storeid, x.FirstOrDefault().userCode, x.FirstOrDefault().orderTime, Count = x.Count(), });

                    foreach (var oneShop in listOrder)
                    {
                        int a = oneShop.Count;
                        PerPriceModel orderModel = new PerPriceModel();
                        orderModel.Date = date11.Date.AddDays(-countData).ToString("yy/MM/dd");
                        orderModel.CustomerSum = oneShop.Count; //单量

                        decimal d = Convert.ToDecimal(oneShop.orderDis);
                        orderModel.Date = date11.Date.ToString("yy/MM/dd");
                        orderModel.PerPrice = ((oneShop.orderSumprice + d) / oneShop.Count).ToString("#.##");
                        orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
                        dataList.Add(orderModel);

                    }
                }
            }
            //按日期查询 跟店铺查询
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid != "" && storeid != null)
            {
                int storeID = Convert.ToInt32(storeid);
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                TimeSpan sp = datetime2.Subtract(datetime1);  //这里验证通过  不在一个月的日期也可以  

                //相差天数
                int dayMin = sp.Days;
                for (var i = 0; i < dayMin; i++)
                {
                    var countData = i;

                    DateTime date11 = datetime1.Date.AddDays(+countData);

                    var query = bll.Search().ToList();

                    var listOrder = query.Where(x => x.orderTime.Date == date11.Date && x.orderStatus == 1 && x.storeid == storeID).GroupBy(x => x.storeid).Select(x => new { x.FirstOrDefault().orderCode, orderSumprice = x.Sum(y => y.orderSumprice), orderDis = x.Sum(y => y.discountMoney), x.FirstOrDefault().storeid, x.FirstOrDefault().userCode, x.FirstOrDefault().orderTime, Count = x.Count(), });

                    foreach (var oneShop in listOrder)
                    {
                        int a = oneShop.Count;
                        PerPriceModel orderModel = new PerPriceModel();
                        orderModel.Date = date11.Date.AddDays(-countData).ToString("yy/MM/dd");
                        orderModel.CustomerSum = oneShop.Count; //单量

                        decimal d = Convert.ToDecimal(oneShop.orderDis);
                        orderModel.Date = date11.Date.ToString("yy/MM/dd");
                        orderModel.PerPrice = ((oneShop.orderSumprice + d) / oneShop.Count).ToString("#.##");
                        orderModel.shopName = storeBll.Search(x => x.storeid == oneShop.storeid).Select(x => new { x.storeid, x.storeName }).ToList().FirstOrDefault().storeName;
                        dataList.Add(orderModel);

                    }
                }
            }

            return Json(dataList, JsonRequestBehavior.AllowGet);
        }

        //sql测试
        public ActionResult PerCustomerPriceBySql(string date1, string date2, string storeid)
        {
            var dataList = new List<PerPriceModel>();
            //   默认查询本天 之前的 一周的记录  总的店铺的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(storeid))
            {
                DateTime datenow = DateTime.Now;
                DateTime datestart = DateTime.Now.AddDays(-6).Date;
                string sql = "select COUNT(*)  as  CustomerSum,  orderTime as Date,sum(discountMoney+orderSumprice) as orderSumprice from usersorder FORCE index(idx_userorder_orderTime) where orderStatus=1 and orderTime BETWEEN '" + datestart + "'  and '" + datenow + "'  GROUP BY DATE(orderTime)";
                var list = bll.SearchBySQL<PerPriceModel>(sql).ToList();
                foreach (var oneShop in list)
                {
                    PerPriceModel orderModel = new PerPriceModel();
                    orderModel.Date = Convert.ToDateTime(oneShop.Date).ToString("MM/dd");
                    orderModel.CustomerSum = oneShop.CustomerSum; //单量
                    orderModel.PerPrice = ((Convert.ToDecimal(oneShop.orderSumprice)) / orderModel.CustomerSum).ToString("#.##");
                    dataList.Add(orderModel);
                }
            }
            //按店铺查询 
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != "" && storeid != null)
            {
                int storeID = Convert.ToInt32(storeid);
                DateTime datenow = DateTime.Now;
                DateTime datestart = DateTime.Now.AddDays(-6).Date;
                string sql = "select COUNT(*)  as  CustomerSum,  orderTime as Date,sum(discountMoney+orderSumprice) as orderSumprice from usersorder FORCE index(idx_userorder_orderTime)  where orderStatus=1 and orderTime BETWEEN '" + datestart + "'  and '" + datenow + "' and storeid=" + storeID + "   GROUP BY DATE(orderTime)";
                var list = bll.SearchBySQL<PerPriceModel>(sql).ToList();
                foreach (var oneShop in list)
                {
                    PerPriceModel orderModel = new PerPriceModel();
                    orderModel.Date = Convert.ToDateTime(oneShop.Date).ToString("MM/dd");
                    orderModel.CustomerSum = oneShop.CustomerSum; //单量
                    orderModel.PerPrice = ((Convert.ToDecimal(oneShop.orderSumprice)) / orderModel.CustomerSum).ToString("#.##");
                    dataList.Add(orderModel);
                }
            }
            //按日期查询 总的
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && string.IsNullOrEmpty(storeid))
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                string sql = "select COUNT(*)  as  CustomerSum,  orderTime as Date,sum(discountMoney+orderSumprice) as orderSumprice from usersorder  FORCE index(idx_userorder_orderStatus) where orderStatus=1 and orderTime BETWEEN '" + datetime1 + "'  and '" + datetime2 + "'  GROUP BY DATE(orderTime)";
                var list = bll.SearchBySQL<PerPriceModel>(sql).ToList();
                foreach (var oneShop in list)
                {
                    PerPriceModel orderModel = new PerPriceModel();
                    orderModel.Date = Convert.ToDateTime(oneShop.Date).ToString("MM/dd");
                    orderModel.CustomerSum = oneShop.CustomerSum; //单量
                    orderModel.PerPrice = ((Convert.ToDecimal(oneShop.orderSumprice)) / orderModel.CustomerSum).ToString("#.##");
                    dataList.Add(orderModel);
                }
            }
            //按日期查询 跟店铺查询
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid != "" && storeid != null)
            {
                int storeID = Convert.ToInt32(storeid);
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                string sql = "select COUNT(*)  as  CustomerSum,  orderTime as Date,sum(discountMoney+orderSumprice) as orderSumprice from usersorder FORCE index(idx_userorder_orderStatus) where orderStatus=1 and orderTime BETWEEN '" + datetime1 + "'  and '" + datetime2 + "'  and storeid=" + storeID + "   GROUP BY DATE(orderTime)";
                var list = bll.SearchBySQL<PerPriceModel>(sql).ToList();
                foreach (var oneShop in list)
                {
                    PerPriceModel orderModel = new PerPriceModel();
                    orderModel.Date = Convert.ToDateTime(oneShop.Date).ToString("MM/dd");
                    orderModel.CustomerSum = oneShop.CustomerSum; //单量
                    orderModel.PerPrice = ((Convert.ToDecimal(oneShop.orderSumprice)) / orderModel.CustomerSum).ToString("#.##");
                    dataList.Add(orderModel);
                }
            }

            return Json(dataList, JsonRequestBehavior.AllowGet);
        }
    }
}
